package org.jivesoftware.smackx.workgroup.agent;

import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.PacketCollector;
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.PacketIDFilter;
import org.jivesoftware.smack.packet.IQ.Type;
import org.jivesoftware.smackx.Form;
import org.jivesoftware.smackx.ReportedData;
import org.jivesoftware.smackx.workgroup.packet.TranscriptSearch;

public class TranscriptSearchManager
{
  private Connection connection;

  public TranscriptSearchManager(Connection paramConnection)
  {
    this.connection = paramConnection;
  }

  public Form getSearchForm(String paramString)
    throws XMPPException
  {
    TranscriptSearch localTranscriptSearch1 = new TranscriptSearch();
    localTranscriptSearch1.setType(IQ.Type.GET);
    localTranscriptSearch1.setTo(paramString);
    PacketCollector localPacketCollector = this.connection.createPacketCollector(new PacketIDFilter(localTranscriptSearch1.getPacketID()));
    this.connection.sendPacket(localTranscriptSearch1);
    TranscriptSearch localTranscriptSearch2 = (TranscriptSearch)localPacketCollector.nextResult(SmackConfiguration.getPacketReplyTimeout());
    localPacketCollector.cancel();
    if (localTranscriptSearch2 == null)
      throw new XMPPException("No response from server on status set.");
    if (localTranscriptSearch2.getError() != null)
      throw new XMPPException(localTranscriptSearch2.getError());
    return Form.getFormFrom(localTranscriptSearch2);
  }

  public ReportedData submitSearch(String paramString, Form paramForm)
    throws XMPPException
  {
    TranscriptSearch localTranscriptSearch1 = new TranscriptSearch();
    localTranscriptSearch1.setType(IQ.Type.GET);
    localTranscriptSearch1.setTo(paramString);
    localTranscriptSearch1.addExtension(paramForm.getDataFormToSend());
    PacketCollector localPacketCollector = this.connection.createPacketCollector(new PacketIDFilter(localTranscriptSearch1.getPacketID()));
    this.connection.sendPacket(localTranscriptSearch1);
    TranscriptSearch localTranscriptSearch2 = (TranscriptSearch)localPacketCollector.nextResult(SmackConfiguration.getPacketReplyTimeout());
    localPacketCollector.cancel();
    if (localTranscriptSearch2 == null)
      throw new XMPPException("No response from server on status set.");
    if (localTranscriptSearch2.getError() != null)
      throw new XMPPException(localTranscriptSearch2.getError());
    return ReportedData.getReportedDataFrom(localTranscriptSearch2);
  }
}